package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.Painter;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.RectF;

public class CompBarra extends Componente {

	final static int BarDensity = 20;
	final static int MinBarSize = 60;
	final static int StepSize = 20;
	final static int InitialBarWidht = 150;
	final static int HORIZONTAL_BAR = 1;
	final static int VERTICAL_BAR = 2;

	private int _currentOrientation;

	public CompBarra() {
		_centerPoint = new Point(0, 0);
		_width = InitialBarWidht;
		_height = BarDensity;
		_currentOrientation = HORIZONTAL_BAR;
		_connectionPoints = new Point[16];
		for (int i = 0; i < _connectionPoints.length; i++) {
			_connectionPoints[i] = new Point();
		}
	}

	public void setOrientation(int value) {
		_currentOrientation = value;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = Anchor.x + offSetX;
		int posY = Anchor.y + offSetY;

		RectF bar = new RectF();
		bar.set(posX * globalScale, posY * globalScale, (posX + _width)
				* globalScale, (posY + _height) * globalScale);
		c.drawRect(bar, Painter.getInstancia().getBarPaint(_currentState));

		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
			}
		}
		return respuesta;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		int cpDist;
		if (_currentOrientation == HORIZONTAL_BAR) {
			cpDist = (int) _width / 9;
			for (int i = 0; i < 8; i++) {
				_connectionPoints[i].set((int) (Anchor.x + ((i + 1) * cpDist)),
						Anchor.y);
			}
			for (int i = 8; i < 16; i++) {
				_connectionPoints[i].set((int) (Anchor.x + ((i - 7) * cpDist)),
						(int) (Anchor.y + _height));
			}
		} else {
			cpDist = (int) _height / 9;
			for (int i = 0; i < 8; i++) {
				_connectionPoints[i].set(Anchor.x,
						(int) (Anchor.y + ((i + 1) * cpDist)));
			}
			for (int i = 8; i < 16; i++) {
				_connectionPoints[i].set((int) (Anchor.x + _width),
						(int) (Anchor.y + ((i - 7) * cpDist)));
			}
		}

		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsBegining) {
				value.CalcularConexiones(this, VistaDiagrama.getVista()
						.getDiagrama().getComponenteById(value._childId));
			}
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsEnd) {
				value.CalcularConexiones(VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(value._fatherId), this);
			}
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		_centerPoint.x = (int) (Anchor.x + (_width / 2));
		_centerPoint.y = (int) (Anchor.y + (_height / 2));
	}

	@Override
	public String generarDOM() {
		String DOM = "\t<Componente Tipo=\"LifeBar\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y +"\" Orientacion=\"" + _currentOrientation +
				"\" Width=\"" + _width +"\" Height=\"" + _height +"\">\n";
		DOM = DOM + "\t</Componente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		// TODO Auto-generated method stub

	}

	@Override
	public Point requestConnectionPoint(CompConector connector, int mode,
			Point otherCompCenter) {
		float distMin = Float.MAX_VALUE;
		float dX = 0;
		float dY = 0;
		float disActual;
		int CP_MasCercano = 0; // Punto de conexion con menor distancia
		for (int i = 0; i < _connectionPoints.length; i++) {
			dX = _connectionPoints[i].x - otherCompCenter.x;
			dY = _connectionPoints[i].y - otherCompCenter.y;
			disActual = (float) Math.sqrt(dX * dX + dY * dY);
			if (disActual < distMin) {
				distMin = disActual;
				CP_MasCercano = i;
			}
		}
		return _connectionPoints[CP_MasCercano];

	}

	@Override
	public void mover(int distX, int distY) {
		Anchor.x += distX;
		Anchor.y += distY;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public String getTipo() {
		return "BarraActividad";
	}

	@Override
	public void setTitulo(String titulo) {
	}

	public void Rotar() {
		if (_currentOrientation == HORIZONTAL_BAR) {
			_currentOrientation = VERTICAL_BAR;
			_height = _width;
			_width = BarDensity;
		} else {
			_currentOrientation = HORIZONTAL_BAR;
			_width = _height;
			_height = BarDensity;
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	public void aumentar() {
		if (_currentOrientation == HORIZONTAL_BAR) {
			_width += StepSize;
		} else {
			_height += StepSize;
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	public void reducir() {
		if (_currentOrientation == HORIZONTAL_BAR) {
			_width -= StepSize;
			if (_width < MinBarSize) {
				_width = MinBarSize;
			}
		} else {
			_height -= StepSize;
			if (_height < MinBarSize) {
				_height = MinBarSize;
			}
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}
	
}
